package com.qida.clm.core.imageloader;

import android.content.Context;
import android.support.annotation.DrawableRes;
import android.widget.ImageView;

import com.android.volley.toolbox.ImageLoader;
import com.baidu.cyberplayer.utils.P;
import com.qida.clm.core.network.NetworkHelp;
import com.qida.clm.utilslib.R;

/**
 * Created by edgar on 2016/2/16.
 */
public class ImageLoaderManager {
    private Context mContext;
    private ImageLoader.ImageCache mImageCache;
    private ImageLoader mImageLoader;
    private ImageConfig mDefaultConfig;
    private static ImageLoaderManager sInstance;

    private ImageLoaderManager(Context context, ImageLoader.ImageCache imageCache,ImageConfig imageConfig){
        this.mContext = context;
        this.mImageCache = imageCache;
        this.mDefaultConfig = imageConfig;
        mImageLoader = new ImageLoader(NetworkHelp.getInstance().getRequestQueue(),
                mImageCache);
    }

    public static synchronized void initialize(Context context, ImageLoader.ImageCache imageCache,ImageConfig imageConfig){
        if(sInstance == null){
            sInstance = new ImageLoaderManager(context,imageCache,imageConfig);
        }
    }

    public static ImageLoaderManager getInstance(){
        return sInstance;
    }

    public void displayNetworkImage(ImageView imageView,String url,ImageConfig imageConfig){
        if(imageView instanceof AsyncLoaderImageView){
            displayNetworkImage((AsyncLoaderImageView) imageView,url,imageConfig);
            return;
        }
        Object tag = imageView.getTag(R.id.image_loader);
        ImageLoaderControl imageLoaderControl;
        if(tag == null){
            imageLoaderControl = new ImageLoaderControl();
            if(imageConfig == null){
                imageConfig = mDefaultConfig;
            }
            if(imageConfig != null){
                imageLoaderControl.setDefaultImageResId(imageConfig.defaultImageId);
                imageLoaderControl.setErrorImageResId(imageConfig.errorImageId);
            }
            imageView.setTag(R.id.image_loader,imageLoaderControl);
            imageView.addOnLayoutChangeListener(imageLoaderControl);
        } else {
            imageLoaderControl = (ImageLoaderControl) tag;
        }
        imageLoaderControl.setImageView(imageView,url,mImageLoader);
    }

    public void displayNetworkImage(AsyncLoaderImageView asyncLoaderImageView,String url,ImageConfig imageConfig){
        if(imageConfig == null){
            imageConfig = mDefaultConfig;
        }
        if(imageConfig != null){
            asyncLoaderImageView.setDefaultImageResId(imageConfig.defaultImageId);
            asyncLoaderImageView.setErrorImageResId(imageConfig.errorImageId);
        }
        asyncLoaderImageView.setImageUrl(url,mImageLoader);
    }

    public static class ImageConfig{
        private final int defaultImageId;
        private final int errorImageId;
        public ImageConfig(@DrawableRes int defaultImageId,@DrawableRes int errorImageId){
            this.defaultImageId = defaultImageId;
            this.errorImageId = errorImageId;
        }
    }
}